'use strict';

module.exports = app => {
  const { INTEGER } = app.Sequelize;

  const ClothingTag = app.model.define('clothing_tag', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    clothing_id: {
      type: INTEGER,
      allowNull: false,
      comment: '衣服ID',
    },
    tag_id: {
      type: INTEGER,
      allowNull: false,
      comment: '标签ID',
    },
    // 时间戳字段由Sequelize自动管理
  }, {
    tableName: 'clothing_tags',
    paranoid: false,  // 禁用软删除
    comment: '衣服标签关联表',
    indexes: [
      {
        fields: ['clothing_id'],
      },
      {
        fields: ['tag_id'],
      },
      {
        unique: true,
        fields: ['clothing_id', 'tag_id'],
      },
    ],
  });

  ClothingTag.associate = function() {
    // 关联表属于衣服
    app.model.ClothingTag.belongsTo(app.model.Clothing, {
      foreignKey: 'clothing_id',
      as: 'clothing',
    });

    // 关联表属于标签
    app.model.ClothingTag.belongsTo(app.model.Tag, {
      foreignKey: 'tag_id',
      as: 'tag',
    });
  };

  return ClothingTag;
};
